package com.wk.springboot.adapter;

import java.util.Map;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;

/**
 * @Date: 2019/1/3 19:28
 * @Description: SearchHitV561Adapter
 */
public class SearchHitV561Adapter implements ISearchHitAdapter {

    private SearchHit searchHit;

    public SearchHitV561Adapter(SearchHit searchHit) {
        this.searchHit = searchHit;
    }

    @Override
    public Map<String, Object> getSource() {
        return searchHit.getSourceAsMap();
    }

    @Override
    public String getFieldValue(String fieldName) {
            SearchHitField field = searchHit.getFields().get(fieldName);
            if (field == null) {
                return "";
            }
            return field.getValue();
        }

    @Override
    public String getId() {
        return searchHit.getId();
    }

    @Override
    public String getIndex() {
        return searchHit.getIndex();
    }

    @Override
    public String getType() {
        return searchHit.getType();
    }

    @Override
    public float getScore() {
        return searchHit.getScore();
    }

    @Override
    public String getScoreStr() {
        return String.valueOf(searchHit.getScore());
    }

    @Override
    public String getSourceAsString() {
        return searchHit.getSourceAsString()
                .replace("\n", "")
                .replace("\r", "");
    }
}
